//
//  AppDelegate.swift
//  01sinablog
//
//  Created by ZhangZiang on 15/12/12.
//  Copyright © 2015年 ZhangZiang. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
//        window = UIWindow(frame : UIScreen.mainScreen().bounds)
        window = UIWindow()
//        window?.backgroundColor = UIColor.whiteColor()
        window?.frame = UIScreen.mainScreen().bounds
        window?.makeKeyAndVisible()
        
        //设置nav和tab的颜色,需要尽早设置
        setupThemeColor()
        
        window?.rootViewController = defaultViewController()
//        window?.rootViewController = PictureSelectViewController()
        
        registerNotification()
        
        
        return true
    }
    //设置tabBar和navigationBar的渲染颜色为橘色
    func setupThemeColor()
    {
        UITabBar.appearance().tintColor = themeColor
        UINavigationBar.appearance().tintColor = themeColor
    }
    
    // MARK: 页面间的跳转
    
    //注册通知,如果收到就跳转相应的界面(一对多,用通知)
    //@objc是用字符串找方法时候要加上(target - action)
    private func registerNotification()
    {
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "switchRootViewController:", name: WBSwitchRootViewController, object: nil)
    }
    @objc func switchRootViewController(n : NSNotification)
    {
        if n.object == nil
        {
            window?.rootViewController = MainViewController()
        }
        else
        {
        window?.rootViewController = WelcomeViewController()
        }
//        guard let dict = n.object else
//        {
//        window?.rootViewController = MainViewController()
//            return
//        }
//        
//        let vcName = dict["VCName"] as! String
//        if vcName == "NewFeature"
//        {
//            if UserAccountViewModel().isUserLogin == true
//            {
//                window?.rootViewController = WelcomeViewController()
//            }
//            else
//            {
//                window?.rootViewController = MainViewController()
//            }
//        }
    }
    //因为是单例,一直存在,所以不用消除通知监听了
//    deinit {
//        NSNotificationCenter.defaultCenter().removeObserver(self)
//    }
    
    //默认应该去的界面
    private func defaultViewController() -> UIViewController
    {
        if isNewVersion()
        {
            return NewFeatureCollectionViewController()
        }
        else
        {
            let userlogin = UserAccountViewModel().isUserLogin
            if userlogin == true
            {
                return WelcomeViewController()
            }
            return MainViewController()
        }
    }
    //判断是否是新版本
    private func isNewVersion() -> Bool
    {
        let dict = NSBundle.mainBundle().infoDictionary
        
        let versionStr = dict!["CFBundleShortVersionString"] as! String
        
        let versionNum = Double(versionStr)
        
        //获得用户惯用
        let userDefault = NSUserDefaults.standardUserDefaults()
        
        if versionNum > userDefault.doubleForKey("VersionString")
        {
            //如果是新版本,需要在本地保存下
            userDefault.setDouble(versionNum!, forKey: "VersionString")
            userDefault.synchronize()
            return true
        }
        else
        {
            return false
        }
    }

    
}

